package com.example.demo.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

@Data
public class AppResult<T> {

    @JsonInclude(JsonInclude.Include.ALWAYS)  // 序列化时包含null值
    private int code;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private String message;
    @JsonInclude(JsonInclude.Include.ALWAYS)
    private T data;



    public static AppResult success() {
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }

    //自定义内容
    public static AppResult success(String message) {
        return new AppResult(ResultCode.SUCCESS.getCode(), message);
    }

    //泛型
    public static <T> AppResult<T> success(T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    public static <T>AppResult<T> success(String message, T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message, data);
    }

    public static AppResult failed(ResultCode failedArticleNotExists){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }

    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(), message);
    }

    public static <T>AppResult<T> failed(T data){
        return new AppResult<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), data);
    }

    public static <T>AppResult<T> failed(String message,T data){
        return new AppResult<>(ResultCode.FAILED.getCode(), message, data);
    }











    public AppResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public AppResult(int code, String message) {
        this(code, message, null);
    }


}
